package com.guoteng.coolweather.util;

import android.text.TextUtils;

import com.guoteng.coolweather.db.City;
import com.guoteng.coolweather.db.County;
import com.guoteng.coolweather.db.Province;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zhaoguoteng on 2017/2/13.
 */

public class Utility {

    /*
    * 解析和处理服务器返回的省级数据
    * */
    public static boolean handleProvinceResponse(String response) {

        if (!TextUtils.isEmpty(response)) {

            try {

                JSONArray allProvinces = new JSONArray(response);

                for (int i = 0; i < allProvinces.length(); i++) {

                    JSONObject provinceObject = allProvinces.getJSONObject(i);

                    Province province = new Province();
                    province.setProvinceName(provinceObject.getString("name"));
                    province.setProvinceCode(provinceObject.getInt("id"));

                    province.save();
                }

                return true;
            }catch (JSONException e) {

                e.printStackTrace();
            }
        }

        return false;
    }

    /*
    * 解析和处理服务器返回的城市数据
    * */
    public static boolean handleCityResponse(String response, int provinceId) {

        if (!TextUtils.isEmpty(response){

            try {

                JSONArray allCitys = new JSONArray(response);

                for (int i = 0; i < allCitys.length(); i++) {

                    JSONObject cityObject = allCitys.getJSONObject(i);

                    City city = new City();

                    city.setCityName(cityObject.getString("name"));
                    city.setId(cityObject.getInt("id"));
                    city.setProvinceId(provinceId);

                    city.save();
                }

                return true;
            }catch (JSONException e) {

                e.printStackTrace();
            }

        }

        return false;
    }

/*
* 解析和处理服务器返回的县级数据
* */

    public static boolean handleCountyResponse(String response, int cityId) {

        if (!TextUtils.isEmpty(response)) {

            try {

                JSONArray allCountys = new JSONArray(response);

                for (int i = 0; i < allCountys.length(); i++) {

                    JSONObject countyObject = allCountys.getJSONObject(i);

                    County county = new County();

                    county.setId(countyObject.getInt("id"));
                    county.setCountyName(countyObject.getString("name"));
                    county.setWeatherId(countyObject.getInt("weather_id"));
                    county.setCityId(cityId);

                    county.save();
                }

                return true;
            }catch (JSONException e) {

                e.printStackTrace();
            }
        }
    }


}









